package com.why.controller;

import com.why.result.Result;
import com.why.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@RestController
@RequestMapping("/api/file")
@Slf4j
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        log.info("***上传头像***");
        //将文件的内容存储到本地磁盘-本项目中的file文件夹中
        String originalFileName = file.getOriginalFilename();//获取文件名
        //保证文件的名字是唯一的，防止文件覆盖
        String fileName = UUID.randomUUID().toString()+originalFileName.substring(originalFileName.lastIndexOf("."));

        //本地存储：绝对路径
        //file.transferTo(new File("E:\\IDEAworkplace\\BigEvent\\big_event\\big_event_server\\src\\main\\resources\\file\\"+fileName));

        //阿里云OSS
        String url = AliOssUtil.uploadFile(fileName,file.getInputStream());
        return Result.success(url);
    }
}
